//
//  HMStatusViewModel.swift
//  SinaWeibo
//
//  Created by apple on 16/9/26.
//  Copyright © 2016年 itcast. All rights reserved.
//  处理cell要显示的数据
// 就是为了控制器瘦身

import UIKit

class HMStatusViewModel: NSObject {
    
    
    //通过一个存储型的属性 给cell提供直接需要的数据
    var avatarImage: UIImage?
    var memberImage: UIImage?
    var iconURL: URL?
    
    
    var status: HMStatus? {
        didSet {
            dealAvatarImage()
            dealMemberImage()
            dealHeadURL()
        }
    }
    
    //cell不能够直接显示显示的数据 都可以在viewmodel中处理
    private func dealHeadURL() {
        let urlString = status?.user?.avatar_large ?? ""
        iconURL = URL(string: urlString)
    }
    private func dealAvatarImage() {
        let verified_type = status?.user?.verified_type ?? -1
        switch verified_type {
        case 0:
            avatarImage = #imageLiteral(resourceName: "avatar_vip")
        case 2,3,5:
            avatarImage = #imageLiteral(resourceName: "avatar_enterprise_vip")
        case 220:
            avatarImage = #imageLiteral(resourceName: "avatar_grassroot")
            
        default:
            avatarImage = nil
        }
    }
    
    //处理用户等级的图片
    func dealMemberImage() {
        //在if let/var 语句中赋值对象后面跟 ',' ','就可以直接使用前面赋值的对象
        if let mbrank = status?.user?.mbrank, mbrank > 0 && mbrank < 7 {
            let imageName = "common_icon_membership_level\(mbrank)"
            memberImage = UIImage(named: imageName)
        }
    }
}
